<?php

namespace App\Ordersys\Repositories;

use App\Ordersys\Contracts\OrderApplyInterface;
use App\Ordersys\Exceptions\OrdersysException;
use App\Ordersys\Models\OrdersysChannelModel;
use App\Ordersys\Models\OrdersysChannelWxusersModel;
use Illuminate\Support\Facades\Request;

class DbOrdersysChannelRepository implements OrdersysChannelInterface
{

    /**
     * 订单渠道
     * @return OrdersysChannelModel
     */
    public function getOrdersysChannelModel()
    {
        return new OrdersysChannelModel();
    }

    public function getOrdersysChannelWxusersModel()
    {
        return new OrdersysChannelWxusersModel();
    }

    /**
     * 获取订单渠道
     * @param $where
     * @return mixed
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'list_order' : $request->input("sort");
        $order = empty($request->input("order")) ? 'asc' : $request->input("order");
        $select = empty($request->input("select")) ? '*' : explode(",", $request->input("select"));
        return $this->getOrdersysChannelModel()->newQuery()->select($select)->where('status', 1)->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 保存订单渠道
     * @param $data
     * @return mixed
     */
    public function saveData($data)
    {
        try {
            if ($r = $this->getOrdersysChannelModel()->newQuery()->updateOrCreate($data)) {
                //新增微信号
                $wxNums = $data['wx_nums'] ?? [];
                foreach ($wxNums as $k => $v) {
                    $userData = [];
                    $userData['channel_id'] = $r;
                    $userData['wxusers'] = $v;
                    $this->getOrdersysChannelWxusersModel()->newQuery()->where($userData)->update(['status' => -1]);
                    $userData['status'] = 1;
                    $this->getOrdersysChannelWxusersModel()->newQuery()->updateOrCreate($userData);
                }
                return true;
            }
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        } catch (\Exception $e) {
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }


    }


    /**
     * 保存订单渠道
     * @param $data
     * @return mixed
     */
    public function editData($data)
    {
        try {
            if ($r = $this->getOrdersysChannelModel()->newQuery()->updateOrCreate($data)) {
                //新增微信号
                $wxNums = $data['wx_nums'] ?? [];
                foreach ($wxNums as $k => $v) {
                    $userData = [];
                    $userData['channel_id'] = $r;
                    $userData['wxusers'] = $v;
                    $this->getOrdersysChannelWxusersModel()->newQuery()->where($userData)->update(['status' => -1]);
                    $userData['status'] = 1;
                    $this->getOrdersysChannelWxusersModel()->newQuery()->updateOrCreate($userData);
                }
                return true;
            }
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        } catch (\Exception $e) {
            throw new OrdersysException(OrdersysException::EROOR_OPTION_NOT);
        }
    }

    /**
     * 获取公告单条信息
     * @param $id
     * @return mixed
     */
    public function getInfo($id)
    {
        $channelInfo = $this->getOrdersysChannelModel()->newQuery()->where("id", $id)->first();
        if (!empty($channelInfo)) {
            $channelInfo['wxusers'] = $this->getOrdersysChannelWxusersModel()->newQuery()->where("id", $id)->pluck('wxusers');
        }
        return $channelInfo;
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($data)
    {
        $id = $data['id'];
        $channelOne = $this->getOrdersysChannelWxusersModel()->newQuery()->where("id", $id)->first();
        if (!empty($channelOne)) {
            throw new OrdersysException(OrdersysException::EROOR_LUCK_NOTEROOR_IEXT);
        }
        return $this->getOrdersysChannelModel()->newQuery()->where('id', $id)->delete();
    }

    /**
     * 查询全部数据
     */
    public function getAllList($where)
    {
        return $this->getOrdersysChannelModel()->newQuery()->where($where)->get();
    }
}